package com.gy.zpchat.ui.chat.room;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;

import im.zego.zim.callback.ZIMRoomJoinedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMRoomFullInfo;
import im.zego.zim.enums.ZIMErrorCode;
import com.gy.zpchat.R;
import com.gy.zpchat.SDKManager;
import com.gy.zpchat.ui.BaseActivity;

public class ZIMRoomJoinActivity extends BaseActivity {

    private TextView roomIDText;

    public static void actionStart(Activity activity) {
        Intent intent = new Intent(activity, ZIMRoomJoinActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_join);
        roomIDText = findViewById(R.id.room_id);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onJoinRoomChat(View view) {
        String roomID = roomIDText.getText().toString();

        SDKManager.sharedInstance().joinRoom(roomID, new ZIMRoomJoinedCallback() {
            @Override
            public void onRoomJoined(ZIMRoomFullInfo roomInfo, ZIMError errorInfo) {
                if (errorInfo.code != ZIMErrorCode.SUCCESS) {
                    Toast.makeText(ZIMRoomJoinActivity.this, "Failed to join group chat: " + errorInfo.code, Toast.LENGTH_LONG).show();
                } else {
                    ZIMRoomChatActivity.actionStart(ZIMRoomJoinActivity.this, roomID, roomInfo.baseInfo.roomName,"");
                }
            }
        });
    }

}
